import com.ruoyi.common.utils.StringUtils;

import javax.validation.constraints.NotNull;
import java.io.File;
import java.text.MessageFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Classname BatchDeleteTable
 * @Author: sunluquan
 * @Date: 2022/4/25 10:26
 * @Description TODO
 */
public class BatchDeleteTable {

    public static Set<String> NEEDLESS = null;
    public static List<String> CLEAR_CONTENT_TEMPLATE_LIST = null;
    public static String SERVICE_NAME = "I{0}Service.java";
    public static String SERVICE_SIMPLE_NAME = "{0}ServiceImpl.java";
    public static String BEAN_NAME = "{0}.java";
    public static String MAPPER_JAVA_NAME = "{0}Mapper.java";
    public static String MAPPER_XML_NAME = "{0}Mapper.xml";
    public static String CONTROLLER_NAME = "{0}Controller.java";
    public static String JS_FOLDER = "{1}";
    public static String HTML_FOLDER = "{1}";

    static {
        CLEAR_CONTENT_TEMPLATE_LIST = Stream.of(
                SERVICE_NAME,SERVICE_SIMPLE_NAME,BEAN_NAME,MAPPER_XML_NAME,MAPPER_JAVA_NAME,CONTROLLER_NAME,JS_FOLDER,HTML_FOLDER
        ).collect(Collectors.toList());

        NEEDLESS = new HashSet<>();
        NEEDLESS.add("test");
        NEEDLESS.add(".idea");
        NEEDLESS.add("target");
        NEEDLESS.add("rouyi-cache");
        NEEDLESS.add("ruoyi-activity");
        NEEDLESS.add("ruoyi-common");
        NEEDLESS.add("ruoyi-generator");
        NEEDLESS.add("ruoyi-system");
        NEEDLESS.add("ruoyi-quartz");
        NEEDLESS.add("ruoyi-framework");
    }

    public static String getBusinessName(String prefix,String tableName){
        if(StringUtils.isNotEmpty(prefix) && tableName.startsWith(prefix)){
            return tableName.replaceFirst(prefix,"");
        }
        return tableName;
    }
    public static void clear(String prefix,@NotNull String tableName){

        Set<String> clearContentSet = new HashSet<>();

        String businessName = StringUtils.convertToCamelCase(getBusinessName(prefix, tableName));
        for (String templateName : CLEAR_CONTENT_TEMPLATE_LIST) {
            clearContentSet.add(MessageFormat.format(templateName,tableName,businessName));
        }

        String projectRootPath = System.getProperty("user.dir");
        File projectRootFile = new File(projectRootPath);
        File[] files = projectRootFile.listFiles();
        for (File file : files) {
            //根目录下的文件 以及忽略的文件夹 不做处理
            if(NEEDLESS.contains(file.getName()) || file.isFile()){
                continue;
            }
            clear(file.listFiles(),clearContentSet);
        }
    }

    public static void clear(File[] files,Set<String> clearContentSet){
        for(File file : files){
            if(NEEDLESS.contains(file.getName()))continue;
            if(StringUtils.isEmpty(clearContentSet))break;
            String name = file.getName();

            if(clearContentSet.contains(name)|| clearContentSet.contains(name=StringUtils.convertToCamelCase(name))){
                clearContentSet.remove(name);

                deleteDir(file);
                continue;
            }
            if(file.isDirectory()){
                clear(file.listFiles(),clearContentSet);
            }
        }
    }

    /**
     * 迭代删除文件夹
     * @param file 文件对象
     */
    public static void deleteDir(File file)
    {
        String name = file.getName();
        if(file.isFile())
        {
            System.out.println(name);
            System.out.println("delete:"+file.delete());
        }else
        {
            File[] files = file.listFiles();
            if(files == null)
            {
                System.out.println(name);
                file.delete();
            }else
            {
                for (int i = 0; i < files.length; i++)
                {
                    deleteDir(files[i]);
                }
                System.out.println(name);
                System.out.println("delete:"+file.delete());
            }
        }
    }

    public static void main(String[] args) {


        clear("","SysSerialNumber");

//        String projectRootPath = System.getProperty("user.dir");
//
//        File file = new File(projectRootPath);
//
//        Properties properties = System.getProperties();
//        Enumeration<Object> keys = properties.keys();
//        while(keys.hasMoreElements()){
//            Object o = keys.nextElement();
//            System.out.println(MessageFormat.format("name:{0},value:{1}",o,properties.getProperty(o.toString())));
//        }
    }
}
